<cfcomponent extends="Model" output="false">

	<cffunction name="init">
		<!--- Filters --->
		<cfset beforeDelete("checkTeams")>
		<cfset beforeCreate("saveNoRounds,saveEndSeasonDate")>
		<!--- Relations --->
		<cfset hasMany("Team")>
		<cfset hasMany(name="Fixture",foreignKey="SeasonId")>
		<!--- Validation --->		
		 <cfset validatesPresenceOf(properties="seasonName,noTeams,startDate")>
		 <cfset validatesLengthOf(properties="seasonName", minimum=3)>
		 <cfset validatesNumericalityOf(property="noTeams", onlyInteger=true)>
	</cffunction>
	
	<cffunction name="checkTeams" hint="Check if there are any teams still associated to this season">
		<cfset teams = model("Team").findAll(where="SeasonID='#this.seasonID#'",select="SeasonID")>
		<cfif teams.recordCount>
			<cfset addErrorToBase(message="Unable to delete, there are relations to teams still!")>
			<cfreturn false>
		</cfif>
	</cffunction>
	
	<cffunction name="saveEndSeasonDate" hint="Saves the date of the last round of the season since the user inputs the number of rounds it is auto generated">
		<cfset this.EndDate = DateAdd("ww",this.NoRounds,this.StartDate)>
		<cfreturn true>
	</cffunction>
	
	<cffunction name="saveNoRounds" hint="No. of rounds is auto generated by the teams in the season">
		<cfset this.noRounds = (this.noTeams - 1) * 2>
		<cfreturn true>
	</cffunction>	

</cfcomponent>
